from django.db import models
from django.contrib.auth.models import AbstractUser
from utils.models import BaseModel


class User(AbstractUser):
    mobile = models.CharField(max_length=11, verbose_name='手机号')
    email_active = models.BooleanField(default=False, verbose_name='邮箱状态')
    address = models.ForeignKey('Address', on_delete=models.SET_NULL, null=True, blank=True, related_name='user_address', verbose_name='默认地址')

    class Meta:
        db_table = 'user'
        verbose_name = '用户表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return User.username


# 收货地址表
class Address(BaseModel):
    receiver = models.CharField(max_length=20, verbose_name='收货人')
    mobile = models.CharField(max_length=11, verbose_name='手机号')
    title = models.CharField(max_length=20, verbose_name='地址名称')
    address = models.CharField(max_length=100, verbose_name='详细地址')
    tel = models.CharField(max_length=20, null=True, blank=True, verbose_name='固定电话')
    email = models.CharField(max_length=30, null=True, blank=True, verbose_name='邮箱')
    province = models.ForeignKey('areas.Area', on_delete=models.PROTECT, related_name='address_province', verbose_name='省')
    city = models.ForeignKey('areas.Area', on_delete=models.PROTECT, related_name='address_city', verbose_name='市')
    district = models.ForeignKey('areas.Area', on_delete=models.PROTECT, related_name='address_district', verbose_name='区')
    user = models.ForeignKey('User', on_delete=models.CASCADE, related_name='address_user', verbose_name='用户')
    is_delete = models.BooleanField(default=False, verbose_name='逻辑删除')

    class Meta:
        db_table = 'address'
        verbose_name = '收货地址表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.receiver
